using System;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class ControllerPlatformMobileSlope : ControllerGeneric
{
	private static float unit = 0.25f;

	public static float platformMobileSlopeHeight = 0.5f;

	public float length = 10f;

	public float height = 6f;

	public float width = 1f;

	public GameObject prisma1;

	public GameObject prisma2;

	public GameObject cub;

	public GameObject prisma3;

	public GameObject prisma4;

	public float unghiRampa;

	public UnityEngine.Object controllerStartPrefab;

	public UnityEngine.Object controllerFinishPrefab;

	public GameObject start;

	public GameObject finish;

	public float startDelay;

	public float speed = 1f;

	public float pause;

	private float widthOld;

	private float heightOld;

	private float lengthOld;

	public override void OnLevelInPlace(Transform levelTransform)
	{
		base.OnLevelInPlace(levelTransform);
		if (levelTransform == base.transform.root)
		{
			PrepareForGame();
		}
	}

	public void PrepareForGame()
	{
		SetNames();
		SetParenting();
		PlatformMobile platformMobile = cub.AddComponent<PlatformMobile>();
		platformMobile.startPositionOffset = start.transform.position - base.transform.position;
		platformMobile.finishPositionOffset = finish.transform.position - base.transform.position;
		platformMobile.delay = startDelay;
		platformMobile.pause = pause;
		platformMobile.speed = speed;
		platformMobile.dependencies = new List<GameObject>();
		platformMobile.dependenciesOffsets = new List<Vector3>();
		platformMobile.dependencies.Add(prisma1);
		platformMobile.dependenciesOffsets.Add(prisma1.transform.position - cub.transform.position);
		platformMobile.dependencies.Add(prisma2);
		platformMobile.dependenciesOffsets.Add(prisma2.transform.position - cub.transform.position);
		platformMobile.dependencies.Add(prisma3);
		platformMobile.dependenciesOffsets.Add(prisma3.transform.position - cub.transform.position);
		platformMobile.dependencies.Add(prisma4);
		platformMobile.dependenciesOffsets.Add(prisma4.transform.position - cub.transform.position);
		base.name = "will_destroy_" + base.name;
		if (Application.isPlaying)
		{
			UnityEngine.Object.Destroy(start);
			UnityEngine.Object.Destroy(finish);
			UnityEngine.Object.Destroy(base.gameObject);
		}
		else
		{
			UnityEngine.Object.DestroyImmediate(start);
			UnityEngine.Object.DestroyImmediate(finish);
			UnityEngine.Object.DestroyImmediate(base.gameObject);
		}
	}

	private void Update()
	{
		if (Application.isEditor && !Application.isPlaying)
		{
			SetupRemoteControl();
			if (IsRemoteControlActive())
			{
				base.transform.position = remoteControl.transform.position;
				base.transform.rotation = remoteControl.transform.rotation;
				length = remoteControl.transform.localScale.z;
				height = remoteControl.transform.localScale.y;
				width = remoteControl.transform.localScale.x;
			}
			if (length < platformMobileSlopeHeight)
			{
				length = platformMobileSlopeHeight;
			}
			if (height < 2f * platformMobileSlopeHeight)
			{
				height = 2f * platformMobileSlopeHeight;
			}
			if (width < 0.0001f)
			{
				width = 0.0001f;
			}
			if (remoteControl != null)
			{
				remoteControl.transform.localScale = new Vector3(width, height, length);
			}
			Setup();
			SetupUVs();
		}
	}

	private void SetParenting()
	{
		for (int childCount = base.transform.childCount; childCount > 0; childCount = base.transform.childCount)
		{
			base.transform.GetChild(0).parent = base.transform.parent;
		}
	}

	private void SetNames()
	{
		cub.name = "Platform Mobile Slope";
		prisma1.name = "Platform Mobile Slope Prisma 1";
		prisma2.name = "Platform Mobile Slope Prisma 2";
		prisma3.name = "Platform Mobile Slope Prisma 3";
		prisma4.name = "Platform Mobile Slope Prisma 4";
	}

	private void Setup()
	{
		AssureParenting();
		if (widthOld != width || heightOld != height || lengthOld != length)
		{
			widthOld = width;
			heightOld = height;
			lengthOld = length;
			Vector3 zero = Vector3.zero;
			Vector3 one = Vector3.one;
			Vector3 zero2 = Vector3.zero;
			float num = Mathf.Sqrt(length * length + (height - 2f * platformMobileSlopeHeight) * (height - 2f * platformMobileSlopeHeight));
			float num2 = Mathf.Sqrt(num * num - platformMobileSlopeHeight * platformMobileSlopeHeight);
			float num3 = Mathf.Acos(length / num) * 57.29578f;
			float num4 = Mathf.Acos(num2 / num) * 57.29578f;
			float num5 = num3 + num4;
			zero = new Vector3(0f, height * 0.5f, length * 0.5f);
			one.z = num2 / unit;
			one.y = platformMobileSlopeHeight / unit;
			one.x = width / unit;
			zero2.x = 0f - num5;
			float num6 = (height - platformMobileSlopeHeight) / Mathf.Sin(num5 * ((float)Math.PI / 180f));
			float num7 = num6 - num2;
			float num8 = num7 * Mathf.Sin(num5 * ((float)Math.PI / 180f));
			float num9 = platformMobileSlopeHeight * Mathf.Cos((90f - num5) * ((float)Math.PI / 180f));
			Vector3 one2 = Vector3.one;
			one2.y = num7 / unit;
			one2.z = platformMobileSlopeHeight / unit;
			one2.x = width / unit;
			if (cub != null)
			{
				cub.transform.localPosition = zero - new Vector3(0f, height / 2f, length / 2f);
				cub.transform.localEulerAngles = zero2;
				cub.transform.localScale = one;
			}
			if (prisma1 != null)
			{
				prisma1.transform.localPosition = Vector3.zero - new Vector3(0f, height / 2f, length / 2f);
				prisma1.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
				prisma1.transform.localScale = one2;
			}
			if (prisma2 != null)
			{
				prisma2.transform.localPosition = new Vector3(0f, num8, num9) - new Vector3(0f, height / 2f, length / 2f);
				prisma2.transform.localEulerAngles = new Vector3(90f + num5, 180f, 0f);
				prisma2.transform.localScale = one2;
			}
			if (prisma3 != null)
			{
				prisma3.transform.localPosition = new Vector3(0f, height - num8, length - num9) - new Vector3(0f, height / 2f, length / 2f);
				prisma3.transform.localEulerAngles = new Vector3(-90f + num5, 180f, 0f);
				prisma3.transform.localScale = one2;
			}
			if (prisma4 != null)
			{
				prisma4.transform.localPosition = new Vector3(0f, height, length) - new Vector3(0f, height / 2f, length / 2f);
				prisma4.transform.localEulerAngles = new Vector3(-90f, 0f, 0f);
				prisma4.transform.localScale = one2;
			}
		}
	}

	public void SetupRemoteControl()
	{
		if (remoteControl == null)
		{
			InitRemoteControl();
			remoteControl.gameObject.name = remoteControl.gameObject.name + base.name;
			remoteControl.gameObject.transform.position = base.transform.position;
			remoteControl.gameObject.transform.rotation = base.transform.rotation;
			remoteControl.gameObject.transform.localScale = new Vector3(width, height, length);
			PresetsPlatformMobileSlope presetsPlatformMobileSlope = remoteControl.gameObject.AddComponent<PresetsPlatformMobileSlope>();
			presetsPlatformMobileSlope.SetupControllerStart(controllerStartPrefab);
			presetsPlatformMobileSlope.SetupControllerFinish(controllerFinishPrefab);
			presetsPlatformMobileSlope.controllerStart.remoteControl.transform.position = base.transform.position + Vector3.back * length;
			remoteControl.SetPlacementParent(presetsPlatformMobileSlope.controllerStart.remoteControl);
			remoteControl.alignX = BoxAlignment.Center;
			remoteControl.alignY = BoxAlignment.RightIn;
			remoteControl.alignZ = BoxAlignment.RightOut;
			presetsPlatformMobileSlope.controllerFinish.remoteControl.SetPlacementParent(presetsPlatformMobileSlope.controllerStart.remoteControl);
			presetsPlatformMobileSlope.controllerFinish.remoteControl.alignX = BoxAlignment.Center;
			presetsPlatformMobileSlope.controllerFinish.remoteControl.alignY = BoxAlignment.RightIn;
			presetsPlatformMobileSlope.controllerFinish.remoteControl.alignZ = BoxAlignment.RightOut;
			presetsPlatformMobileSlope.controllerFinish.remoteControl.alignZOffset = length;
		}
	}

	public override void CopyFromRemoteControl(RemoteControl rc)
	{
		base.CopyFromRemoteControl(rc);
		ControllerPlatformMobileSlope component = rc.controller.GetComponent<ControllerPlatformMobileSlope>();
		PresetsPlatformMobileSlope component2 = remoteControl.gameObject.GetComponent<PresetsPlatformMobileSlope>();
		PresetsPlatformMobileSlope component3 = rc.gameObject.GetComponent<PresetsPlatformMobileSlope>();
		remoteControl.alignX = BoxAlignment.None;
		remoteControl.alignY = BoxAlignment.None;
		remoteControl.alignZ = BoxAlignment.None;
		component2.controllerFinish.remoteControl.alignX = BoxAlignment.None;
		component2.controllerFinish.remoteControl.alignY = BoxAlignment.None;
		component2.controllerFinish.remoteControl.alignZ = BoxAlignment.None;
		component2.SetPositionControllerStart(component.start.transform.position);
		component2.SetPositionControllerFinish(component.finish.transform.position);
		remoteControl.SetPosition(rc.transform.position);
		component2.controllerStart.remoteControl.SetPlacementParent(component3.controllerStart.remoteControl.placementParent);
		component2.controllerStart.remoteControl.alignX = component3.controllerStart.remoteControl.alignX;
		component2.controllerStart.remoteControl.alignY = component3.controllerStart.remoteControl.alignY;
		component2.controllerStart.remoteControl.alignZ = component3.controllerStart.remoteControl.alignZ;
		component2.controllerStart.remoteControl.alignXOffset = component3.controllerStart.remoteControl.alignXOffset;
		component2.controllerStart.remoteControl.alignYOffset = component3.controllerStart.remoteControl.alignYOffset;
		component2.controllerStart.remoteControl.alignZOffset = component3.controllerStart.remoteControl.alignZOffset;
		if (component.remoteControl.placementParent == component3.controllerStart.remoteControl)
		{
			remoteControl.SetPlacementParent(component2.controllerStart.remoteControl);
		}
		else
		{
			remoteControl.SetPlacementParent(component.remoteControl.placementParent);
		}
		remoteControl.alignX = component.remoteControl.alignX;
		remoteControl.alignY = component.remoteControl.alignY;
		remoteControl.alignZ = component.remoteControl.alignZ;
		remoteControl.alignXOffset = component.remoteControl.alignXOffset;
		remoteControl.alignYOffset = component.remoteControl.alignYOffset;
		remoteControl.alignZOffset = component.remoteControl.alignZOffset;
		if (component3.controllerFinish.remoteControl.placementParent == component3.controllerStart.remoteControl)
		{
			component2.controllerFinish.remoteControl.SetPlacementParent(component2.controllerStart.remoteControl);
		}
		else
		{
			component2.controllerFinish.remoteControl.SetPlacementParent(component3.controllerFinish.remoteControl.placementParent);
		}
		component2.controllerFinish.remoteControl.alignX = component3.controllerFinish.remoteControl.alignX;
		component2.controllerFinish.remoteControl.alignY = component3.controllerFinish.remoteControl.alignY;
		component2.controllerFinish.remoteControl.alignZ = component3.controllerFinish.remoteControl.alignZ;
		component2.controllerFinish.remoteControl.alignXOffset = component3.controllerFinish.remoteControl.alignXOffset;
		component2.controllerFinish.remoteControl.alignYOffset = component3.controllerFinish.remoteControl.alignYOffset;
		component2.controllerFinish.remoteControl.alignZOffset = component3.controllerFinish.remoteControl.alignZOffset;
		startDelay = component.startDelay;
		speed = component.speed;
		pause = component.pause;
		unghiRampa = component.unghiRampa;
		component2.lockHeight = component3.lockHeight;
		component2.lockLenght = component3.lockLenght;
		component2.angle = component3.angle;
	}

	public void SetupUVs()
	{
		Mesh mesh = prisma1.GetComponent<MeshFilter>().mesh;
		Vector2[] uv = mesh.uv;
		float num = 0.0625f;
		float num2 = 2f;
		uv[17].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[17].y = 1f * num2;
		uv[18].x = 0.5f + prisma1.transform.localScale.y * num;
		uv[18].y = 0f * num2;
		uv[19].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[19].y = 0f * num2;
		uv[14].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[14].y = 0f * num2;
		uv[15].x = 0.5f + prisma1.transform.localScale.y * num;
		uv[15].y = 0f * num2;
		uv[16].x = 0.5f - prisma1.transform.localScale.y * num;
		uv[16].y = 1f * num2;
		num = 1f / 32f;
		num2 = 1f / 32f;
		float num3 = 0.5f + cub.transform.localScale.z * num2 + prisma2.transform.localScale.y * num2;
		uv[8].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[8].y = num3;
		uv[9].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[9].y = num3 + prisma1.transform.localScale.y * num2;
		uv[10].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[10].y = num3;
		uv[11].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[11].y = num3 + prisma1.transform.localScale.y * num2;
		uv[12].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[12].y = num3;
		uv[13].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[13].y = num3 + prisma1.transform.localScale.y * num2;
		uv[0].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[0].y = 0.5f + prisma1.transform.localScale.z * num2;
		uv[1].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[1].y = 0.5f - prisma1.transform.localScale.z * num2;
		uv[2].x = 0.5f - prisma1.transform.localScale.x * num;
		uv[2].y = 0.5f - prisma1.transform.localScale.z * num2;
		uv[3].x = 0.5f + prisma1.transform.localScale.x * num;
		uv[3].y = 0.5f + prisma1.transform.localScale.z * num2;
		mesh.uv = uv;
		mesh = prisma2.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		uv[14].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[14].y = 0f * num2;
		uv[15].x = 0.5f + prisma2.transform.localScale.y * num;
		uv[15].y = 0f * num2;
		uv[16].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[16].y = 1f * num2;
		uv[17].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[17].y = 1f * num2;
		uv[18].x = 0.5f + prisma2.transform.localScale.y * num;
		uv[18].y = 0f * num2;
		uv[19].x = 0.5f - prisma2.transform.localScale.y * num;
		uv[19].y = 0f * num2;
		num = 1f / 32f;
		num2 = 1f / 32f;
		num3 = 0.5f + cub.transform.localScale.z * num2;
		uv[8].x = 0.5f - prisma2.transform.localScale.x * num;
		uv[8].y = num3 + prisma2.transform.localScale.y * num2;
		uv[9].x = 0.5f + prisma2.transform.localScale.x * num;
		uv[9].y = num3;
		uv[10].x = 0.5f + prisma2.transform.localScale.x * num;
		uv[10].y = num3 + prisma2.transform.localScale.y * num2;
		uv[11].x = 0.5f + prisma2.transform.localScale.x * num;
		uv[11].y = num3;
		uv[12].x = 0.5f - prisma2.transform.localScale.x * num;
		uv[12].y = num3 + prisma2.transform.localScale.y * num2;
		uv[13].x = 0.5f - prisma2.transform.localScale.x * num;
		uv[13].y = num3;
		mesh.uv = uv;
		mesh = prisma3.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		uv[17].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[17].y = 0f * num2;
		uv[18].x = 0.5f + prisma3.transform.localScale.y * num;
		uv[18].y = 1f * num2;
		uv[19].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[19].y = 1f * num2;
		uv[14].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[14].y = 1f * num2;
		uv[15].x = 0.5f + prisma3.transform.localScale.y * num;
		uv[15].y = 1f * num2;
		uv[16].x = 0.5f - prisma3.transform.localScale.y * num;
		uv[16].y = 0f * num2;
		num = 1f / 32f;
		num2 = 1f / 32f;
		num3 = 0.5f + cub.transform.localScale.z * num2;
		uv[8].x = 0.5f - prisma3.transform.localScale.x * num;
		uv[8].y = num3 + prisma3.transform.localScale.y * num2;
		uv[9].x = 0.5f + prisma3.transform.localScale.x * num;
		uv[9].y = num3;
		uv[10].x = 0.5f + prisma3.transform.localScale.x * num;
		uv[10].y = num3 + prisma3.transform.localScale.y * num2;
		uv[11].x = 0.5f + prisma3.transform.localScale.x * num;
		uv[11].y = num3;
		uv[12].x = 0.5f - prisma3.transform.localScale.x * num;
		uv[12].y = num3 + prisma3.transform.localScale.y * num2;
		uv[13].x = 0.5f - prisma3.transform.localScale.x * num;
		uv[13].y = num3;
		mesh.uv = uv;
		mesh = prisma4.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		num = 0.0625f;
		num2 = 2f;
		uv[14].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[14].y = 1f * num2;
		uv[15].x = 0.5f + prisma4.transform.localScale.y * num;
		uv[15].y = 1f * num2;
		uv[16].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[16].y = 0f * num2;
		uv[17].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[17].y = 0f * num2;
		uv[18].x = 0.5f + prisma4.transform.localScale.y * num;
		uv[18].y = 1f * num2;
		uv[19].x = 0.5f - prisma4.transform.localScale.y * num;
		uv[19].y = 1f * num2;
		num = 1f / 32f;
		num2 = 1f / 32f;
		num3 = 0.5f + cub.transform.localScale.z * num2 + prisma3.transform.localScale.y * num2;
		uv[8].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[8].y = num3;
		uv[9].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[9].y = num3 + prisma4.transform.localScale.y * num2;
		uv[10].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[10].y = num3;
		uv[11].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[11].y = num3 + prisma4.transform.localScale.y * num2;
		uv[12].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[12].y = num3;
		uv[13].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[13].y = num3 + prisma4.transform.localScale.y * num2;
		uv[0].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[0].y = 0.5f + prisma4.transform.localScale.z * num2;
		uv[1].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[1].y = 0.5f - prisma4.transform.localScale.z * num2;
		uv[2].x = 0.5f - prisma4.transform.localScale.x * num;
		uv[2].y = 0.5f - prisma4.transform.localScale.z * num2;
		uv[3].x = 0.5f + prisma4.transform.localScale.x * num;
		uv[3].y = 0.5f + prisma4.transform.localScale.z * num2;
		mesh.uv = uv;
		mesh = cub.GetComponent<MeshFilter>().mesh;
		uv = mesh.uv;
		num = 1f / 32f;
		num2 = 1f / 32f;
		uv[4].x = 0.5f + cub.transform.localScale.x * num;
		uv[6].x = 0.5f - cub.transform.localScale.x * num;
		uv[5].x = 0.5f - cub.transform.localScale.x * num;
		uv[7].x = 0.5f + cub.transform.localScale.x * num;
		uv[4].y = 0.5f + cub.transform.localScale.z * num2;
		uv[6].y = 0.5f + cub.transform.localScale.z * num2;
		uv[5].y = 0.5f - cub.transform.localScale.z * num2;
		uv[7].y = 0.5f - cub.transform.localScale.z * num2;
		uv[0].x = 0.5f - cub.transform.localScale.x * num;
		uv[0].y = 0.5f - cub.transform.localScale.z * num2;
		uv[1].x = 0.5f + cub.transform.localScale.x * num;
		uv[1].y = 0.5f + cub.transform.localScale.z * num2;
		uv[2].x = 0.5f - cub.transform.localScale.x * num;
		uv[2].y = 0.5f + cub.transform.localScale.z * num2;
		uv[3].x = 0.5f + cub.transform.localScale.x * num;
		uv[3].y = 0.5f - cub.transform.localScale.z * num2;
		num = 0.0625f;
		num2 = 0.5f;
		uv[16].x = 0.5f + cub.transform.localScale.z * num;
		uv[18].x = 0.5f - cub.transform.localScale.z * num;
		uv[17].x = 0.5f - cub.transform.localScale.z * num;
		uv[19].x = 0.5f + cub.transform.localScale.z * num;
		uv[16].y = 0.5f + num2;
		uv[18].y = 0.5f + num2;
		uv[17].y = 0.5f - num2;
		uv[19].y = 0.5f - num2;
		uv[20].x = 0.5f - cub.transform.localScale.z * num;
		uv[22].x = 0.5f + cub.transform.localScale.z * num;
		uv[21].x = 0.5f + cub.transform.localScale.z * num;
		uv[23].x = 0.5f - cub.transform.localScale.z * num;
		uv[20].y = 0.5f + num2;
		uv[22].y = 0.5f + num2;
		uv[21].y = 0.5f - num2;
		uv[23].y = 0.5f - num2;
		mesh.uv = uv;
	}
}
